﻿using BL_Client.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class Dorms : System.Web.UI.Page
    {
        static BL_Client.IBL bl = new BL_Client.BL();
        static User user;

        public Dorms()
        {
            user = bl.GetUserByEmail(User.Identity.Name);
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (userIsAdmin(user)) //check the role of the user
                this.Page.MasterPageFile = "~/Admin.Master";
            else
                this.Page.MasterPageFile = "~/AvBait.Master";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (userIsAdmin(user))
                jTableScript.InnerHtml = @"<script type=""text/javascript"">
                                            $(document).ready(function () {
                                                $('#DormsTableContainer').jtable({
                                                    title: 'Table of Dorms',
                                                    actions: {
                                                        listAction: '/Dorms.aspx/DormsList',
                                                        createAction: '/Dorms.aspx/CreateDorm',
                                                        updateAction: '/Dorms.aspx/UpdateDorm',
                                                        deleteAction: '/Dorms.aspx/DeleteDorm'
                                                    },
                                                    fields: {
                                                        ID: {
                                                            key: true,
                                                            list: false
                                                        },
                                                        Name: {
                                                            title: 'Dorm Name',
                                                            width: '40%'
                                                        },
                                                        NumberOfFloors: {
                                                            title: 'Number of Floors',
                                                            width: '20%'
                                                        }
                                                    },
                                                    formCreated: function (event, data) {
                                                                    data.form.find('input[name=""Name""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""NumberOfFloors""]').addClass('validate[required,custom[integer,min[1],max[6]]]');
                                                                    data.form.validationEngine();
                                                    },
                                                    formSubmitting: function (event, data) {
                                                        return data.form.validationEngine('validate');
                                                    },
                                                    formClosed: function (event, data) {
                                                        data.form.validationEngine('hide');
                                                        data.form.validationEngine('detach');
                                                    }
                                                });

                                                $('#DormsTableContainer').jtable('load');
                                            });
                                        </script>";
            else
                jTableScript.InnerHtml = @"<script type=""text/javascript"">
                                            $(document).ready(function () {
                                                $('#DormsTableContainer').jtable({
                                                    title: 'Table of Dorms',
                                                    actions: {
                                                        listAction: '/Dorms.aspx/DormsList'
                                                    },
                                                    fields: {
                                                        ID: {
                                                            key: true,
                                                            list: false
                                                        },
                                                        Name: {
                                                            title: 'Dorm Name',
                                                            width: '40%'
                                                        },
                                                        NumberOfFloors: {
                                                            title: 'Number of Floors',
                                                            width: '20%'
                                                        }
                                                    },
                                                    formCreated: function (event, data) {
                                                                    data.form.find('input[name=""Name""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""NumberOfFloors""]').addClass('validate[required,custom[integer,min[1],max[6]]]');
                                                                    data.form.validationEngine();
                                                    },
                                                    formSubmitting: function (event, data) {
                                                        return data.form.validationEngine('validate');
                                                    },
                                                    formClosed: function (event, data) {
                                                        data.form.validationEngine('hide');
                                                        data.form.validationEngine('detach');
                                                    }
                                                });

                                                $('#DormsTableContainer').jtable('load');
                                            });
                                        </script>";
        }

        [WebMethod(EnableSession = true)]
        public static object DormsList()//int jtStartIndex, int jtPageSize, string jtSorting)
        {
            try
            {
                //Get data from database
                List<Dorm> dorms = bl.GetAllDorms().ToList();

                //Return result to jTable
                return new { Result = "OK", Records = dorms, TotalRecordCount = dorms.Count };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }

        }

        [WebMethod(EnableSession = true)]
        public static object CreateDorm(Dorm record)
        {
            if (!userIsAdmin(user))
                return new { Result = "ERROR" };

            try
            {
                if (bl.AddDorm(record))
                {
                    Dorm addedDorm = record;
                    return new { Result = "OK", Record = addedDorm };
                }
                else
                {
                    return new { Result = "ERROR", Message = "Error adding dorm" };
                }
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object UpdateDorm(Dorm record)
        {
            if (!userIsAdmin(user))
                return new { Result = "ERROR" };

            try
            {
                if (bl.UpdateDorm(record))
                    return new { Result = "OK" };
                else
                    return new { Result = "ERROR", Message = "Error editing dorm" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object DeleteDorm(int ID)
        {
            if (!userIsAdmin(user))
                return new { Result = "ERROR" };

            try
            {
                if (bl.RemoveDorm(ID))
                    return new { Result = "OK" };
                else
                    return new { Result = "ERROR", Message = "Cant delete dorm" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        private static bool userIsAdmin(User user)
        {
            return (user.Role == Role.Admin);
        }
    }
}